package com.codeazur.as3swf.tags
{
   import com.codeazur.as3swf.SWFData;
   import flash.display.BitmapData;
   import flash.display.Loader;
   import flash.events.Event;
   import flash.utils.ByteArray;
   
   public class TagDefineBits implements IDefinitionTag
   {
      
      public static const TYPE:uint = 6;
       
      
      public var bitmapType:uint = 1;
      
      protected var _characterId:uint;
      
      protected var _bitmapData:ByteArray;
      
      protected var loader:Loader;
      
      protected var onCompleteCallback:Function;
      
      public function TagDefineBits()
      {
         super();
         this._bitmapData = new ByteArray();
      }
      
      public function get characterId() : uint
      {
         return this._characterId;
      }
      
      public function set characterId(param1:uint) : void
      {
         this._characterId = param1;
      }
      
      public function get bitmapData() : ByteArray
      {
         return this._bitmapData;
      }
      
      public function parse(param1:SWFData, param2:uint, param3:uint, param4:Boolean = false) : void
      {
         this._characterId = param1.readUI16();
         if(param2 > 2)
         {
            param1.readBytes(this._bitmapData,0,param2 - 2);
         }
      }
      
      public function publish(param1:SWFData, param2:uint) : void
      {
         param1.writeTagHeader(this.type,this._bitmapData.length + 2,true);
         param1.writeUI16(this._characterId);
         if(this._bitmapData.length > 0)
         {
            param1.writeBytes(this._bitmapData);
         }
      }
      
      public function clone() : IDefinitionTag
      {
         var _loc1_:TagDefineBits = new TagDefineBits();
         _loc1_.characterId = this.characterId;
         _loc1_.bitmapType = this.bitmapType;
         if(this._bitmapData.length > 0)
         {
            _loc1_.bitmapData.writeBytes(this._bitmapData);
         }
         return _loc1_;
      }
      
      public function exportBitmapData(param1:Function) : void
      {
         this.onCompleteCallback = param1;
         this.loader = new Loader();
         this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.exportCompleteHandler);
         this.loader.loadBytes(this._bitmapData);
      }
      
      protected function exportCompleteHandler(param1:Event) : void
      {
         var _loc2_:Loader = param1.target.loader as Loader;
         var _loc3_:BitmapData = new BitmapData(_loc2_.content.width,_loc2_.content.height);
         _loc3_.draw(_loc2_);
         this.onCompleteCallback(_loc3_);
      }
      
      public function get type() : uint
      {
         return TYPE;
      }
      
      public function get name() : String
      {
         return "DefineBits";
      }
      
      public function get version() : uint
      {
         return 1;
      }
      
      public function get level() : uint
      {
         return 1;
      }
      
      public function toString(param1:uint = 0, param2:uint = 0) : String
      {
         return Tag.toStringCommon(this.type,this.name,param1) + "ID: " + this.characterId + ", " + "BitmapLength: " + this._bitmapData.length;
      }
   }
}
